package com.lanyou.cook.entity.domain.userManagement;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.entity.kettle.KettleUser;
import com.lanyou.cook.repository.FieldSystemRepository;
import com.lanyou.cook.service.ApplicationContextKeeper;

@Entity
@EntityListeners({ AuditingEntityListener.class})
@Table(name = "T_COOK_DB_USER")
public class User {
	
	private String id;	//用户信息表ID USER_ID
	
	private String userCode;	//账号  USER_CODE

	private String userName;	//用户姓名 USER_NAME

	private String password;	//密码 PASSWORD

	private String empNo;	//工号 EMP_NO

	private String phoneNumber;	//电话 PHONE

	private String emailAddress;	//邮箱地址 EAMIL

	private String loginStatus;	//登录状态	LOGIN_STATUS
	
	private int version;	//并发控制字段    VERSION
	
	private String remark;	//备注  REMARK
	
	private String creator;	//创建人    CREATOR
	
	private Date createTime;	//创建时间    CREATED_DATE
	
	private String modifier;	//最后更新人员    MODIFIER
	
	private Date modifyTime;	//最后更新时间    LAST_UPDATED_DATE
	
	private Organization organization;	//用户所属部门
	
	private Role role; //用户角色
	
	private Set<FieldSystem> fieldSystems;
	
	private KettleUser kettleUser;
	
	private String exeTimeOnDay;//每天发送邮件的时间
	
	public final static Specification<User> EmptySpecification = new Specification<User>(){
		@Override
		public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<User> idSpecification(String id){
		return new Specification<User>(){
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	/*@PrePersist
	private void prePersist(){
		loginStatus = UserEnableType.Disable.ValueCode;
	}*/

	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "USER_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "USER_CODE", nullable = false, unique = true, length = 20)
	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	@Column(name = "USER_NAME", nullable = false, length = 10)
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Column(name = "PASSWORD", nullable = false,length = 200)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "EMP_NO", nullable = false, unique = true, length = 20)
	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	@Column(name = "PHONE", length = 20)
	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	@Column(name = "EMAIL", length = 100)
	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	@Column(name = "LOGIN_STATUS",nullable = false, length = 10)
	public String getLoginStatus() {
		return loginStatus;
	}

	public void setLoginStatus(String loginStatus) {
		this.loginStatus = loginStatus;
	}
	
	@Version
	@JsonIgnore
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Column(name = "REMARK", length = 100)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	

	@CreatedBy
	@Column(name = "CREATOR", length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(name = "MODIFIER", length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@ManyToOne
	@JoinColumn(name = "ORG_ID")
	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}
	
	
	@ManyToOne
	@JoinColumn(name = "ROLE_ID")
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@ManyToMany
	@JoinTable(name = "T_COOK_BU_USER_SYSTEM", 
		joinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID") , 
			inverseJoinColumns = @JoinColumn(name = "FIELD_SYSTEM_ID", referencedColumnName = "FIELD_SYSTEM_ID") 
	)
	public Set<FieldSystem> getFieldSystems() {
		return fieldSystems;
	}

	public void setFieldSystems(Set<FieldSystem> fieldSystems) {
		this.fieldSystems = fieldSystems;
	}
	
	@OneToOne(optional=true, cascade = CascadeType.ALL, orphanRemoval = true)
	@JoinColumn(name = "R_USER_ID")	
	public KettleUser getKettleUser() {
		return kettleUser;
	}

	public void setKettleUser(KettleUser kettleUser) {
		this.kettleUser = kettleUser;
	}


	@Transient
	public String getCompanyName() {
		try {
			return (String) PropertyUtils.getProperty(organization, "parent.orgName");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getCompanyId() {
		try {
			return (String) PropertyUtils.getProperty(organization, "parent.id");
		} catch (Exception e) {
			return null;
		}
	}
	
	
	@Transient
	public String getDeptName() {
		try {
			return (String) PropertyUtils.getProperty(organization, "orgName");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getDeptId() {
		try {
			return (String) PropertyUtils.getProperty(organization, "id");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getRoleId() {
		try {
			return (String) PropertyUtils.getProperty(role, "id");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getFieldSystemCount() {
		FieldSystemRepository fieldSystemRepository = ApplicationContextKeeper.getApplicationContext().getBean(FieldSystemRepository.class);
		try {
			/*return String.valueOf(fieldSystems.size());*/
			return String.valueOf(fieldSystemRepository.countByUsers_Id(getId()));
		} catch (Exception e) {
			return null;
		}
	}
	
	
	@Transient
	public String getRoleName() {
		try {
			return (String) PropertyUtils.getProperty(role, "roleName");
		} catch (Exception e) {
			return null;
		}
	}
	

	@Column(name = "EXE_TIME_ON_DAY")
	public String getExeTimeOnDay() {
		return exeTimeOnDay;
	}

	public void setExeTimeOnDay(String exeTimeOnDay) {
		this.exeTimeOnDay = exeTimeOnDay;
	}

}
